package com.genia.toolbox.projects.csv.business.manager.impl;

import com.genia.toolbox.basics.exception.BundledException;
import com.genia.toolbox.projects.csv.bean.Binder;
import com.genia.toolbox.projects.csv.bean.EntityBinder;
import com.genia.toolbox.projects.csv.bean.FieldBinder;
import com.genia.toolbox.projects.csv.bean.impl.BinderVisitorAdapter;
import com.genia.toolbox.projects.csv.business.exception.MappingException;

/**
 * visitor that initialize binders.
 */
public class InitializerBinderVisitor
    extends BinderVisitorAdapter
{

  /**
   * error when a {@link Binder} has no associated class.
   */
  public static final String KEY_NO_ASSOCIATED_CLASS_FOR_BINDER = InitializerBinderVisitor.class.getName() + ".error.NoAssociateClassForBinder";

  /**
   * error when an EntityBinder has not related entity.
   */
  public static final String KEY_ENTITY_BINDER_WITH_NO_ENTITY = InitializerBinderVisitor.class.getName() + ".error.entityBinderWithNoEntity";

  /**
   * error when an FieldBinder has not related field.
   */
  public static final String KEY_FIELD_BINDER_WITH_NO_ENTITY = InitializerBinderVisitor.class.getName() + ".error.fieldBinderWithNoEntity";



  /**
   * common visit method.
   * 
   * @param binder
   *          the binder to visit
   * @throws MappingException
   *           if the mapping is incorrect
   */
  private void visitBinder(Binder binder)
      throws MappingException
  {
    if (binder.getBindedObjectClass() == null) {
      throw new MappingException("a binder for entity " + binder.getEntityDescriptor().getName() + " of folder " + binder.getEntityDescriptor().getFolderDescriptor().getFolderName()
          + " has no binded entity class associated.", KEY_NO_ASSOCIATED_CLASS_FOR_BINDER, binder.getEntityDescriptor().getName(), binder.getEntityDescriptor().getFolderDescriptor().getFolderName());
    }
  }



  /**
   * visit method for {@link EntityBinder}.
   * 
   * @param binder
   *          the binder to visit
   * @throws BundledException
   *           if an error occured
   * @see com.genia.toolbox.projects.csv.bean.impl.BinderVisitorAdapter#visitEntityBinder(com.genia.toolbox.projects.csv.bean.EntityBinder)
   */
  @Override
  public void visitEntityBinder(EntityBinder binder)
      throws BundledException
  {
    visitBinder(binder);
    if (binder.getBindedEntity() == null) {
      throw new MappingException("the entity binder of entity " + binder.getEntityDescriptor().getName() + " of folder " + binder.getEntityDescriptor().getFolderDescriptor().getFolderName()
          + " linked to the entity " + binder.getEntityName() + " that does not exist", KEY_ENTITY_BINDER_WITH_NO_ENTITY, binder.getEntityDescriptor().getFolderDescriptor().getFolderName(), binder
          .getEntityDescriptor().getName(), binder.getEntityName());
    }
  }



  /**
   * visit method for {@link FieldBinder}.
   * 
   * @param binder
   *          the binder to visit
   * @throws BundledException
   *           if an error occured
   * @see com.genia.toolbox.projects.csv.bean.impl.BinderVisitorAdapter#visitEntityBinder(com.genia.toolbox.projects.csv.bean.FieldBinder)
   */
  @Override
  public void visitFieldBinder(FieldBinder binder)
      throws BundledException
  {
    visitBinder(binder);
    if (binder.getBindedField() == null) {
      throw new MappingException("the field binder of entity " + binder.getEntityDescriptor().getName() + " of folder " + binder.getEntityDescriptor().getFolderDescriptor().getFolderName()
          + " linked to the field " + binder.getFieldName() + " that does not exist", KEY_FIELD_BINDER_WITH_NO_ENTITY, binder.getEntityDescriptor().getFolderDescriptor().getFolderName(), binder
          .getEntityDescriptor().getName(), binder.getFieldName());
    }
  }
}